/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
/**
 *
 * @author TuPA
 */
public class GenerateObject {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    //new GenerateObject().GenerateJavaObj("web/schema/products.xsd");
        //GennerateUserSchema();
        //GenerateOrder();
        GenerateOrderDetail();
        GenerateOrderClient();
        
    }
    public static  void GennerateUserSchema(){
        try {
            String output = "src/java";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            sc.forcePackageName("DTO.Object.Users");
            File file = new File("web/schema/userschema_old.xsd");
            InputSource is = new InputSource(file.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Finished");
        } catch (IOException ex) {
            Logger.getLogger(GenerateObject.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public static void GenerateOrder(){
        try {
            String output = "src/java";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            sc.forcePackageName("DTO.Object.OrderUser");
            File file = new File("web/schema/orderuser_old.xsd");
            InputSource is = new InputSource(file.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Order User Finished");
        } catch (IOException ex) {
            Logger.getLogger(GenerateObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void GenerateOrderDetail(){
        try {
            String output = "src/java";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            sc.forcePackageName("DTO.Object.OrderDetail");
            File file = new File("web/schema/orderdetail_old.xsd");
            InputSource is = new InputSource(file.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("OrderDetail Finished");
        } catch (IOException ex) {
            Logger.getLogger(GenerateObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void GenerateJavaObj(String strXSD){
        try{ String output = "src/java";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException e) {
                    System.out.println("error" + e.getMessage());
                }

                public void fatalError(SAXParseException e) {
                    System.out.println("fatal" + e.getMessage());
                }

                public void warning(SAXParseException e) {
                    System.out.println("warning" + e.getMessage());
                }

                public void info(SAXParseException e) {
                    System.out.println("info" + e.getMessage());
                }
            });
            sc.forcePackageName("DTO");
            File schema = new File(strXSD);
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Finished");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static  void GenerateOrderClient()
    {
        try {
            String output = "src/java";
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void fatalError(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void warning(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }

                public void info(SAXParseException saxpe) {
                    throw new UnsupportedOperationException("Not supported yet.");
                }
            });
            sc.forcePackageName("DTO.Object.OrderClient");
            File file = new File("web/Schema/orderClient.xsd");
            InputSource is = new InputSource(file.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Finished");
        } catch (IOException ex) {
            Logger.getLogger(GenerateObject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}